package com.zhongfl.crm.model.boss;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 订单清结算明细
 *
 * @author wang.yuchuan
 * @date 2018/5/14
 */
@Getter
@Setter
public class OrderClearDetailsResult {

    /**
     * 订单信息
     */
    private Order order;

    /**
     * 所有实际收支流水明细
     */
    private List<OrderCapitalDetail> orderCapitalDetails;

    /**
     * 实际收入流水列表
     */
    private List<OrderCapitalDetail> incomeOrderCapitalDetails;

    /**
     * 实际收入合计金额
     */
    private BigDecimal totalIncomeAmount;

    public BigDecimal getTotalIncomeAmount() {
        BigDecimal incomeAmount = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(incomeOrderCapitalDetails)) {
            for (OrderCapitalDetail item : incomeOrderCapitalDetails) {
                incomeAmount = incomeAmount.add(item.getTradeAmount());
            }
        }
        return incomeAmount;
    }

    /**
     * 实际支出流水列表
     */
    private List<OrderCapitalDetail> outcomeOrderCapitalDetails;

    /**
     * 实际支出合计金额
     */
    private BigDecimal totalOutcomeAmount;

    public BigDecimal getTotalOutcomeAmount() {
        BigDecimal outcomeAmount = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(outcomeOrderCapitalDetails)) {
            for (OrderCapitalDetail item : outcomeOrderCapitalDetails) {
                outcomeAmount = outcomeAmount.add(item.getTradeAmount());
            }
        }
        return outcomeAmount;
    }

    /**
     * 订单所有结算单信息
     */
    private List<WorkOrderSettle> workOrderSettles;

    /**
     * 商务挂账清单列表
     */
    private List<WorkOrderSettle> salerChargeSettles;

    public List<WorkOrderSettle> getSalerChargeSettles() {
        if (CollectionUtils.isEmpty(workOrderSettles)) {
            return new ArrayList<>();
        }
        List<WorkOrderSettle> result = workOrderSettles.stream()
            .filter(s -> s.getOutsiteIncomeChargeAmount().compareTo(BigDecimal.ZERO) > 0)
            .collect(Collectors.toList());
        return result;
    }

    /**
     * 挂账总金额
     */
    private BigDecimal totalChargeAmount;

    public BigDecimal getTotalChargeAmount() {
        BigDecimal totalChargeAmount = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(this.getSalerChargeSettles())) {
            for (WorkOrderSettle item : this.getSalerChargeSettles()) {
                totalChargeAmount = totalChargeAmount.add(item.getOutsiteIncomeChargeAmount());
            }
        }
        return totalChargeAmount;
    }


    /**
     * 外部收入明细
     */
    private List<WorkOrderSettle> outsiteIncomeSettles;

    public List<WorkOrderSettle> getOutsiteIncomeSettles() {
        if (CollectionUtils.isEmpty(workOrderSettles)) {
            return new ArrayList<>();
        }
        List<WorkOrderSettle> result = workOrderSettles.stream()
            .filter(s -> s.getOutsiteIncomeChargeAmount().compareTo(BigDecimal.ZERO) > 0
                || s.getOutsiteIncomeReceivedAmount().compareTo(BigDecimal.ZERO) > 0)
            .collect(Collectors.toList());
        return result;
    }

    /**
     * 挂账相关统计
     */
    private ClearRechargeStaticResult rechargeStaticResult;

    public BigDecimal sumChargeAmount(CreditParty creditParty) {
        if (CollectionUtils.isEmpty(this.getSalerChargeSettles())) {
            return BigDecimal.ZERO;
        }

        BigDecimal result = BigDecimal.ZERO;
        for (WorkOrderSettle workOrderSettle : this.getSalerChargeSettles()) {
            if (Objects.nonNull(workOrderSettle.getCreditParty()) && workOrderSettle.getCreditParty().equals(creditParty)) {
                result = result.add(workOrderSettle.getOutsiteIncomeChargeAmount());
            }
        }
        return result;
    }

    public static class ClearRechargeStaticResult {

        /**
         * 总共挂账金额
         */
        @Getter
        @Setter
        private BigDecimal totalRechargeAmount;
        /**
         * 商务挂账金额
         */
        @Getter
        @Setter
        private BigDecimal salerRechargeAmount;

        /**
         * 渠道挂账金额
         */
        @Getter
        @Setter
        private BigDecimal channelRechargeAmount;

        /**
         * 服务费挂账金额
         */
        @Getter
        @Setter
        private BigDecimal serviceFeeRechargeAmount;

        /**
         * 未清算挂账金额
         */
        @Getter
        @Setter
        private BigDecimal unClearAmount;

        /**
         * 最终渠道佣金
         */
        @Getter
        @Setter
        private BigDecimal channelFee;

        /**
         * 订单最终收入
         */
        @Getter
        @Setter
        private BigDecimal orderFinalTotalIncomeAmount;

        /**
         * 清结算备注
         */
        @Getter
        @Setter
        private String remark;
    }
}
